package com.haochang.chunkvod.controller.adapter.hostess;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.view.vod.listView.DragSwapListView;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * @author Yangjie
 *         className SortRoomRoomAdapter
 *         created at  2016/8/24  19:16
 *         开房排序 房间排序
 */
public class SortRoomRoomAdapter extends BaseDragSwapAdapter<SortRoomRoomAdapter.ViewHolder, HostessInfo> implements DragSwapListView.OnDragACTION_UP
{
	private int keyOfTag = R.string.app_name;
	private boolean canExpand = false;
	private Context context;
	private DragSwapListView listView;
	private List<HostessInfo> list;
	public List<HostessInfo> listTemp;
	private long lastActionUpMilles;//最后手指离开时间，防止按的太快
	private boolean recordTimeMilles = false;//是否更新最后手指拿起的时间

	public SortRoomRoomAdapter(Context context, List<HostessInfo> list, DragSwapListView listView)
	{
		super(list);
		this.context = context;
		this.listView = listView;
		this.list = list;
		listTemp = new ArrayList<HostessInfo>();
		listTemp.addAll(list);
		listView.setCallBack(this);
	}

	@Override
	public ViewHolder createViewHolder(LayoutInflater inflater, int position)
	{
		final ViewGroup viewGroup = null;
		View view = inflater.inflate(R.layout.item_sort_room_room, viewGroup, false);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(final Context context, ViewHolder holder, final int position)
	{
		final HostessInfo info = getItem(position);
		holder.tv_room.setText(info.getContent());
		holder.iv_sort_right.setVisibility(info.isShowRightDrawable() ? View.VISIBLE : View.INVISIBLE);
		holder.line_sort_bottom.setVisibility(info.isShowBottom() ? View.VISIBLE : View.GONE);
		if (canExpand)
		{
			holder.rele_sort_top.setClickable(true);
			holder.rele_sort_top.setEnabled(true);
			holder.rele_sort_top.setTag(keyOfTag, position);
			holder.rele_sort_top.setOnClickListener(onBaseClickListener);
		} else
		{
			holder.rele_sort_top.clearFocus();
			holder.rele_sort_top.setClickable(false);
			holder.rele_sort_top.setEnabled(true);
			holder.rele_sort_top.setTag(keyOfTag, position);
			holder.rele_sort_top.setOnTouchListener(onTouchListener);
		}
		holder.line_sort_up.setTag(keyOfTag, position);
		holder.line_sort_up.setOnClickListener(onBaseClickListener);
		holder.line_sort_down.setTag(keyOfTag, position);
		holder.line_sort_down.setOnClickListener(onBaseClickListener);

	}

	/**
	 * 置顶 置底点击事件 //显示隐藏
	 */
	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			int position = (Integer) v.getTag(keyOfTag);
			switch (v.getId())
			{
			//点击item下半部分，置底
			case R.id.line_sort_down:
				if (position == list.size() - 1)
				{
					ToastUtils.showText(context, "已经在最后一项");
				} else
				{
					final HostessInfo info = list.get(position);
					info.setShowBottom(false);
					list.remove(position);
					list.add(list.size(), info);
					notifyDataSetChanged();
					if (context instanceof PositionChangedCallBack)
					{
						((PositionChangedCallBack) context).returnList(list, hasDataChanged());
					}
				}
				break;
			//点击item上半部分，置顶
			case R.id.line_sort_up:
				if (0 == position)
				{
					ToastUtils.showText(context, "已经在第一项");
				} else
				{
					final HostessInfo info = list.get(position);
					info.setShowBottom(false);
					list.remove(position);
					list.add(0, info);
					notifyDataSetChanged();
					if (context instanceof PositionChangedCallBack)
					{
						((PositionChangedCallBack) context).returnList(list, hasDataChanged());
					}
				}
				break;
			//上半部分点击 ，显示/隐藏下方的
			case R.id.rele_sort_top:
				final HostessInfo info = getItem(position);
				int len = list.size();
				if (len > 0)
				{
					for (int i = 0; i < len; i++)
					{
						HostessInfo info1 = list.get(i);
						if (info1.isShowBottom() && position != i)
						{
							info1.setShowBottom(false);
						}
					}
				}
				info.setShowBottom(!info.isShowBottom());
				notifyDataSetChanged();
				break;
			default:
				break;
			}
		}
	};
	View.OnTouchListener onTouchListener = new View.OnTouchListener()
	{
		@Override
		public boolean onTouch(View v, MotionEvent motionEvent)
		{
			int position = (Integer) v.getTag(keyOfTag);
			if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
			{
				if (System.currentTimeMillis() - lastActionUpMilles >= 500 || lastActionUpMilles == 0)
				{
					recordTimeMilles = true;
					listView.startShadow(position);
				} else
				{
					recordTimeMilles = false;
				}
			}
			return false;
		}
	};

	/**
	 *  判断操作（置顶，拖拽）过程中是否有变化，拖拽之后listTemp有变化，不是元数据，不能作为最后数据变化来判断
	 */
	public boolean hasDataChanged()
	{
		int len = list.size();
		for (int i = 0; i < len; i++)
		{
			HostessInfo info = list.get(i);
			HostessInfo infoTemp = listTemp.get(i);
			if (info == null || infoTemp == null)
			{
				return false;
			}
			if (!TextUtils.equals(list.get(i).getRoomId(), listTemp.get(i).getRoomId()))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * 设置listview item 是否可展开下半部分  
	 * @param canExpand 设置是否可展开
	 */
	public void setCanExpand(boolean canExpand)
	{
		this.canExpand = canExpand;
		notifyDataSetChanged();
	}

	@Override
	public void onActionUp()
	{
		Logger.e("onActionUp");
		if (recordTimeMilles)
		{
			lastActionUpMilles = System.currentTimeMillis();
		}
	}

	class ViewHolder extends BaseDragSwapAdapter.BaseViewHolder
	{
		public BaseTextView tv_room;//文字设置
		public ImageView iv_sort_right;//右边排序icon
		public RelativeLayout rele_sort_top;//布局上半部分
		public LinearLayout line_sort_bottom;//布局下半部分
		public LinearLayout line_sort_up;//置顶
		public LinearLayout line_sort_down;//最后

		protected ViewHolder(View mView)
		{
			super(mView);
			tv_room = (BaseTextView) mView.findViewById(R.id.tv_room);
			iv_sort_right = (ImageView) mView.findViewById(R.id.iv_sort_right);
			rele_sort_top = (RelativeLayout) mView.findViewById(R.id.rele_sort_top);
			line_sort_bottom = (LinearLayout) mView.findViewById(R.id.line_sort_bottom);
			line_sort_up = (LinearLayout) mView.findViewById(R.id.line_sort_up);
			line_sort_down = (LinearLayout) mView.findViewById(R.id.line_sort_down);
		}
	}

	public interface PositionChangedCallBack
	{
		void returnList(List<HostessInfo> list, boolean dataHasChanged);
	}
}
